package com.suning.controller;

import com.suning.command.BlogCommand;
import com.suning.entity.Blog;
import com.suning.service.BlogService;
import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 苏宁软件技术有限公司 版权所有
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/")
public class BlogController extends BaseController {

    @Autowired
    private BlogService blogService;
    /**
     * 搜索数据入口
     * @param request
     * @param cmd
     * @return
     */
    @RequestMapping(value = { "/init" }, method = RequestMethod.GET)
    public String searchMain(HttpServletRequest request, @ModelAttribute("cmd") BlogCommand cmd) {
        return "blog/init";
    }

    /**
     * 搜索数据入口
     * @param request
     * @param cmd
     * @return
     */
    @RequestMapping(value = { "/search" }, method = RequestMethod.GET)
    public String searchResult(HttpServletRequest request, @ModelAttribute("cmd") BlogCommand cmd,ModelMap modelMap) throws Exception {
        List<Blog> blogList = blogService.query(cmd.getTag(), cmd.getCurrentPage(), cmd.getPageSize());
        cmd.setTotal(CollectionUtils.isNotEmpty(blogList)?blogList.get(0).getTotal() : 0);
        cmd.setBlog(blogList);
        return "blog/result";
    }


    /**
     * 数据详情入口
     * @param request
     * @param cmd
     * @return
     */
    @RequestMapping(value = { "/more" }, method = RequestMethod.GET)
    public String searchMore(HttpServletRequest request, @ModelAttribute("cmd") BlogCommand cmd) {

        return "blog/more";
    }


    /**
     * 数据录入入口
     * @param request
     * @param cmd
     * @return
     */
    @RequestMapping(value = { "/input" }, method = RequestMethod.GET)
    public String searchDataInput(HttpServletRequest request, @ModelAttribute("cmd") BlogCommand cmd) {

        return "blog/input";
    }

}
